package com.carl.zlds.tool.util;

import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringUtil {
    private static final Logger log = LoggerFactory.getLogger(com.carl.zlds.tool.util.StringUtil.class);
    private static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String NUMBER_CHAR = "0123456789";

    public StringUtil() {
    }

    public static Integer parseInt(Object obj, Integer optional) {
        if (obj instanceof Long) {
            return ((Long)obj).intValue();
        } else if (obj instanceof Integer) {
            return (Integer)obj;
        } else {
            try {
                return (int)Double.parseDouble(obj.toString());
            } catch (Exception var3) {
                log.error("parse Integer error: {}, source: \n{}", var3.getMessage(), obj);
                return optional;
            }
        }
    }

    public static Integer parseInt(Object obj) {
        return parseInt(obj, 0);
    }

    public static Long parseLong(Object obj, Long optional) {
        if (obj instanceof Long) {
            return (Long)obj;
        } else if (obj instanceof Integer) {
            return ((Integer)obj).longValue();
        } else {
            try {
                return (long)Double.parseDouble(obj.toString());
            } catch (Exception var3) {
                log.error("parse Long error: {}, source: \n{}", var3.getMessage(), obj);
                return optional;
            }
        }
    }

    public static Long parseLong(Object obj) {
        return parseLong(obj, 0L);
    }

    public static Double parseDouble(Object obj, Double optional) {
        if (obj instanceof Double) {
            return (Double)obj;
        } else {
            try {
                return Double.parseDouble(obj.toString());
            } catch (Exception var3) {
                log.error("parse Double error: {}, source: \n{}", var3.getMessage(), obj);
                return optional;
            }
        }
    }

    public static Double parseDouble(Object obj) {
        return parseDouble(obj, 0.0D);
    }

    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();

        for(int i = 0; i < length; ++i) {
            sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(random.nextInt("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".length())));
        }

        return sb.toString();
    }

    public static String generateMixString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();

        for(int i = 0; i < length; ++i) {
            sb.append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(random.nextInt("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".length())));
        }

        return sb.toString();
    }

    public static String generateLowerString(int length) {
        return generateMixString(length).toLowerCase();
    }

    public static String generateUpperString(int length) {
        return generateMixString(length).toUpperCase();
    }

    public static String generateNumberString(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for(int i = 0; i < length; ++i) {
            sb.append("0123456789".charAt(random.nextInt("0123456789".length())));
        }

        return sb.toString();
    }
}